//
//  ViewController.swift
//  MyNews
//
//  Created by DF-Mac on 16/11/30.
//  Copyright © 2016年 DF-Mac. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var newsVC = NewsViewController()
    
    var tf1: UITextField?
    var tf2: UITextField?
    
    var  resultLabel :UILabel?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
   
        setupUI()
        caculate()
    }
    func caculate() -> () {
        guard let num1=Int(tf1?.text ?? ""),let num2=Int(tf2?.text ?? "") else {
            print("输入必须int")
            return
        }
        resultLabel?.text="\(num1+num2)"
    }
    func setupUI() -> Void {
//        let  textField1=UITextField(frame: CGRect(x: 0, y: 100, width: 100, height: 30))
//        textField1.borderStyle = .roundedRect
//        textField1.text="0"
        let textField1 = UITextField(frame: CGRect(x: 0, y: 100, width: 100, height: 30), placeholder: "0", fontSize: 14)
        
        view.addSubview(textField1)
        
        let textField2 = UITextField(frame: CGRect(x: 120, y: 100, width: 100, height: 30), placeholder: "0", fontSize: 14)
//        textField2.text = "0"
//        textField2.borderStyle = .roundedRect
        view.addSubview(textField2)
        
        tf1=textField1
        tf2=textField2
        
        let l1 = UILabel(frame: CGRect(x: 100, y: 100, width: 20, height: 30))
        l1.text="+"
        l1.textAlignment = .center
        
        view.addSubview(l1)
        
        let l2 = UILabel(frame: CGRect(x: 220, y: 100, width: 20, height: 30))
        l2.text="="
        l2.textAlignment = .center
        
        view.addSubview(l2)
        
        let l3 = UILabel(frame: CGRect(x: 240, y: 100, width: 60, height: 30))
        l3.text="0"
        l3.textAlignment = .center
        
        view.addSubview(l3)
        resultLabel=l3
        //未分装之前的按钮
//        let btn = UIButton()
//        btn.setTitle("brn", for: .normal)
//        btn.setTitleColor(UIColor.green, for: .normal)
//        btn.sizeToFit()
//        
//        btn.center=view.center
//        
//        view.addSubview(btn)
//
//        btn.addTarget(self, action: #selector(caculate), for: .touchUpInside)
        
//        let btn = UIButton()
//        btn.setTitle("brn", for: .normal)
//        btn.setTitleColor(UIColor.green, for: .normal)
//        btn.sizeToFit()
        //封装之后的按钮
        let btn = UIButton(title: "confirm", titleColor: .cyan)
        
        btn.center=view.center
        
        view.addSubview(btn)
        
        btn.addTarget(self, action: #selector(caculate), for: .touchUpInside)
        
        //新闻按钮
        let newsbtn = UIButton(title: "newsButton", titleColor: .green)
        let rect = CGRect(origin: CGPoint(x:200,y:300),size: CGSize(width: 100, height: 50))
        newsbtn.frame(forAlignmentRect: rect)
        view.addSubview(newsbtn)
        
        newsbtn.addTarget(self, action: #selector(showNewsVC), for: .touchUpInside)
    }
    func loadData(completed:@escaping (([String])->())) -> () {
        DispatchQueue.global().async {
            print("\(Thread.current)")
            
            let json = ["111","222","333"]
            DispatchQueue.main.async(execute: { 
                print("zhu(\(Thread.current))")
                completed(json)
            })
        }
    }
    
    //点击屏幕空白部分或者news按钮跳转到newsVC
    func showNewsVC() -> () {
        self.navigationController?.pushViewController(newsVC, animated: true)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//        UINavigationController.init(rootViewController: self)

        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

